#region Summary

// 
// Owner : JackieHan 
// 
// Solution : UpdateSystem
// 
// Project :Project
// 
// File : UpdateSystemInfo.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 某个待更新的系统的全局配置
// 
// History : 
//			2011-01-05 10:23:06	    Create 
// 

#endregion

#region Using

using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Xml;

#endregion

namespace Jackie.Update.Entities.Server
{
    /// <summary> 
    /// 某个待更新的系统的全局配置
    /// </summary>
    public class UpdateSystemInfo
    {
        private readonly string configPath;

        public UpdateSystemInfo(XmlNode node, string configDirectoryPath)
        {
            var systemName = node.Attributes["name"].Value;
            var publishPath = node.Attributes["publishPath"].Value;
            configPath = Path.Combine(configDirectoryPath, node.Attributes["configFile"].Value);

            if (!File.Exists(configPath))
                throw new FileNotFoundException("file not found!", configPath);

            var doc = new XmlDocument();
            doc.Load(configPath);

            var publishNodes = doc.GetElementsByTagName("Publish");
            var publishLogs = (from XmlNode publishNode in publishNodes
                               select new PublishLogInfo(publishNode)).ToDictionary(publishLog => publishLog.SystemVersion);

            PublishPath = publishPath;
            SystemName = systemName;
            PublishLogs = publishLogs;
        }

        /// <summary>
        /// 系统名称
        /// </summary>
        public string SystemName { get; set; }
        /// <summary>
        /// 发布目录
        /// </summary>
        public string PublishPath { get; set; }
        /// <summary>
        /// 发布历史
        /// </summary>
        public IDictionary<string, PublishLogInfo> PublishLogs { get; set; }

        /// <summary>
        /// 根据系统版本号获取该版本号的发布记录
        /// </summary>
        /// <param name="version">发布时的版本号</param>
        /// <returns></returns>
        public PublishLogInfo this[string version]
        {
            get
            {
                return PublishLogs[version];
            }
        }

        /// <summary>
        /// 获取当前系统的最新的文件
        /// </summary>
        /// <returns></returns>
        public List<UpdateFileInfo> GetNewFiles()
        {
            var files = new List<UpdateFileInfo>();
            var logs = PublishLogs.Values.OrderByDescending(log => log.Date);

            foreach (var log in logs)
            {
                foreach (var file in log.Files)
                {
                    if (files.All(item => file.Path != item.Path))
                        files.Add(file);
                }
            }

            return files.Where(f => f.OperateType != OperateType.Delete).ToList();
        }

        public void SaveToFile(PublishLogInfo log)
        {
            var doc = new XmlDocument();
            doc.Load(configPath);

            var rootElement = doc.DocumentElement;

            if (rootElement == null)
            {
                var publishsElement = doc.CreateElement("Publishs");
                doc.AppendChild(publishsElement);
                rootElement = publishsElement;
            }
            rootElement.AppendChild(log.CreateConfigNode(doc));

            doc.Save(configPath);
        }
    }
}